package com.xsq.rpc.common.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName ZookeeperConf
 * @Description TODO
 * @Author xsq
 * @Date 2020/6/4 17:16
 **/
@Configuration
public class ZookeeperConf {

    @Autowired
    private CuratorZookeeperProperties curatorZookeeperProperties;

    /**
     * 连接Zookeeper
     *
     * @return
     */
    @Bean
    public CuratorFramework getCuratorFramework() throws Exception {
        System.out.println("开启zookeeper" + curatorZookeeperProperties.getConnectString()+"--------maxRetries："+curatorZookeeperProperties.getMaxRetries());
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(curatorZookeeperProperties.getBaseSleepTimeMs(), curatorZookeeperProperties.getMaxRetries());
        CuratorFramework client = CuratorFrameworkFactory.newClient(curatorZookeeperProperties.getConnectString(), curatorZookeeperProperties.getSessionTimeoutMs(), curatorZookeeperProperties.getConnectionTimeoutMs(), retryPolicy);
        client.start();
        System.out.println("开启zookeeper成功");
        return client;
    }
}